//
//  ActiveViewController.m
//  A段项目
//
//  Created by dlios on 15/7/17.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "ActiveViewController.h"
#import "SANetWorkingTask.h"
#import "ActivityTableViewCell.h"
#import "MJRefresh.h"
#import "RecSecViewController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "BaseHeader.h"


@interface ActiveViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArr;
@property (nonatomic,assign) NSInteger detailsPageCount;

@end

@implementation ActiveViewController

- (void)dealloc
{
   
    [_table release];
    [_tableArr release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.tableArr = [NSMutableArray array];
        self.detailsPageCount = 2;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTable];
    // Do any additional setup after loading the view.
}

#pragma mark -
#pragma mark 创建table
- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 5, ScreenWidth, ScreenHeidth - 100) style:UITableViewStylePlain];
    _table.dataSource = self;
    _table.delegate = self;
    _table.rowHeight = 300;
    [_table registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"a"];
    [self.view addSubview:_table];
    [_table release];
    [self.table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(loadNewData:)];
    
    [SANetWorkingTask request:@"http://api.hd.bitauto.com/baojia/?appid=540ff6b5db742&appkey=b5de92a2c74ade114c08e6ee63f01210&city_id=1708&device_id=B00000612AE35EF525E5838D3B7A5C0A&device_type=2&page_num=1&result_count=20&token=8bd8bd0887d34967394fc97db0da9ba1" parmater:nil block:^(NSDictionary *result) {
        NSDictionary *bigDic = [result objectForKey:@"data"];
        NSArray *arr = [bigDic objectForKey:@"items"];
        [_tableArr addObjectsFromArray:arr];
        [_table reloadData];
    }];
    
    
}


#pragma mark -
#pragma mark 下拉刷新

- (void)loadNewData:(id)sender
{
    [self.table reloadData];
    [self.table.header endRefreshing];
}


#pragma mark -
#pragma mark table的协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.tableArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    cell.target = self;
    cell.action = @selector(target:);
    cell.button.tag = indexPath.row;
    NSDictionary *dic = [_tableArr objectAtIndex:indexPath.row];
    [cell.show sd_setImageWithURL:[dic objectForKey:@"images"] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.title.text = [dic objectForKey:@"title"];
    cell.sponsor.text = [dic objectForKey:@"sponsor"];
    NSArray *arr = [[dic objectForKey:@"start_date"] componentsSeparatedByString:@" "];
    cell.start.text = [arr objectAtIndex:0];
    arr = [[dic objectForKey:@"end_date"] componentsSeparatedByString:@" "];
    cell.end.text = [arr objectAtIndex:0];
    return cell;
}

- (void)target:(id)sender
{
    UIButton *bu = (UIButton *)sender;
    NSDictionary *dic = [_tableArr objectAtIndex:bu.tag];
    NSString *url = [dic objectForKey:@"url"];
    RecSecViewController *web = [[RecSecViewController alloc] init];
    web.str = url;
    [self.navigationController pushViewController:web animated:YES];
//    [self presentViewController:web animated:YES completion:nil];
    [web release];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [_tableArr objectAtIndex:indexPath.row];
    NSString *url = [dic objectForKey:@"url"];
    RecSecViewController *web = [[RecSecViewController alloc] init];
    web.str = url;
    [self.navigationController pushViewController:web animated:YES];
    [web release];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
